#ifndef _BASE_JSON_PARSER_H_
#define _BASE_JSON_PARSER_H_

#include <string>
#include <vector>
#include <map>

#include "singleton.h"
#include "rapidjson/document.h"
#include <boost/algorithm/string.hpp>

namespace base {

class JsonParser : public Singleton<JsonParser> {
public:
  //解析形如{"key1":..., "key2":..., "key3":[{"subkey1":..., ...},...}，有层级的key用.相连
  bool parse(const std::string & jsonData, std::map<std::string, std::string> & mapData);

  //解析形如[{"key1":...,...}, {"key2":...,}, {"key3":[{"subkey1":..., ...},...]...}]，有层级的key用.相连
  bool parseSeq(const std::string & jsonData, std::vector<std::map<std::string, std::string> > & seqData);

private:
  friend class Singleton<JsonParser>;
  JsonParser() {}

  void parseMember(const rapidjson::Value::Member & member, std::map<std::string, std::string> & mapData,
      std::string keyPrefix);

  void filterIllegalChars(std::string& s) {
    std::string filterChars = "\t\r\n\v\b";
    for (size_t i = 0; i < filterChars.size(); ++i) {
      boost::algorithm::erase_all(s, filterChars.substr(i, i+1));
    }
  }
};

}

#endif // _BASE_JSON_PARSER_H_

